package logic

import (
	"context"
	"day1/day1rpc/day1rpc"
	"day1/day1rpc/internal/svc"
	"day1/model/mysql"
	"day1/model/redisinit"
	"fmt"
	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLoginLogic {
	return &RegisterLoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 注册登录
func (l *RegisterLoginLogic) RegisterLogin(in *day1rpc.RegisterLoginRequest) (*day1rpc.RegisterLoginResponse, error) {
	// todo: add your logic here and delete this line

	get := redisinit.Rdb.Get("sendSms" + "login" + in.Tel)
	if get.Val() != in.SendSms {
		return nil, fmt.Errorf("验证码不正确")
	}
	var user mysql.User
	mysql.DB.Debug().Where("tel = ?", in.Tel).Find(&user)
	if user.ID == 0 {
		m := mysql.User{
			Tel: in.Tel,
		}
		m.CreateUser()
	}
	mysql.DB.Debug().Where("tel = ?", in.Tel).Find(&user)
	return &day1rpc.RegisterLoginResponse{
		Id: int64(user.ID),
	}, nil
}
